package controller

import (
	"fmt"
	"hft/Database/admin"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type ModelActivity struct{}

func (*ModelActivity) CreateModelActivity(c *gin.Context) {

	var data *admin.ModelActivity

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ModelActivityService.CreateModelActivity(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*ModelActivity) EditModelActivity(c *gin.Context) {

	var data *admin.ModelActivity

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ModelActivityService.EditModelActivity(claims.BaseClaims, data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*ModelActivity) GetAllModel(c *gin.Context) {
	var data utils.Page
	c.ShouldBind(&data)
	list, err := services.ModelActivityService.GetAllModel(&data)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.OkWithData(list, c)
}

func (*ModelActivity) GetModelActivityList(c *gin.Context) {
	id, _ := strconv.Atoi(c.DefaultQuery("city", "0"))
	fmt.Println(id)
	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.ModelActivityService.GetModelActivityList(claims.BaseClaims, id)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*ModelActivity) GetRegion(c *gin.Context) {
	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.ModelActivityService.GetRegion(claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

func (*ModelActivity) GetModelActivityListInLouPan(c *gin.Context) {
	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.ModelActivityService.GetModelActivityListInLouPan(id, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

var ModelActivityController = new(ModelActivity)
